<template>
  <div>
    <FormDialogComponent
      ref="formtDialogRef"
      dialogWidth="400px"
      v-model="form"
      :formInline="false"
      dialogTitle="整车审核"
      :autoTitle="false"
      :formHeader="formHeader"
      :formUrl="`${urls.API_URL_BACKSTAGE_MES_PRODUCTIONORDER}/inspection`"
      :showFormFoot="false"
      formAction="patch"
      @dialogClosed="dialogClosed"
      @beforeFormSubmit="beforeFormSubmit"
      @afterFormSubmitFailure="afterFormSubmitFailure"
      @afterSubmitSuccess="afterSubmitSuccess"
    >
    </FormDialogComponent>
  </div>
</template>

<script setup lang="ts">
import FormDialogComponent from '@/components/base/table/form/FormDialogComponent.vue';
import MaterialBatchSelectComponent from '@/components/wms/MaterialBatchSelectComponent.vue';
import ProductionOrderStepTable from '@/views/page/mes/productionorder/ProductionOrderStepTable.vue';
import type { FormHeader, TableData } from '@/types/interface/base_interface';
import { nextTick, onMounted, ref } from 'vue';
import urls from '@/api/request/urls';
import Request from '@/api/request/request';
import type { ProductionOrder, ProductionOrderStep } from '@/types/interface/mes_interface';
import { getDict } from '@/api/dict';
import type { MaterialBatch } from '@/types/interface/wms_interface';
const emit = defineEmits(['afterSubmitSuccess']);

// 接收父容器参数
interface Props {
  action?: string; // 表单的处理类型，post表示提交，put表示编辑
}
const props = withDefaults(defineProps<Props>(), {
  action: 'put'
});

let productionOrderId: string | undefined; //生产订单ID

//表单==========================================================
//表单对象
const form = ref<ProductionOrder>({});
//表单元素类型
const formHeader = ref<FormHeader[]>([]);
onMounted(async () => {
  //下拉框
  const isInspectionPassedDict = await getDict('审核状态', 'boolean');
  // 添加编辑表单
  formHeader.value = [
    {
      label: '审核结果',
      prop: 'auditResult',
      type: 'select',
      list: isInspectionPassedDict
    },
    {
      label: '审核意见',
      prop: 'auditNote',
      placeholder: ' ',
      type: 'textarea'
    }
  ];
});
//===================================对话框====================================================
const formtDialogRef = ref();
//初始化表单信息
const initForm = () => {
  //传入初始值
  if (form.value) {
    //如果物料批次存在，则设置物料名
    // if (form.value.materialBatch != null) {
    //   form.value.materialName = form.value.materialBatch.material!.name;
    // }
  }
};
//加载生产订单
// const loadProductionOrderStep = async (id: string) => {
//   const resData: any = await new Request(`${urls.API_URL_BACKSTAGE_MES_PRODUCTIONORDERSTEP}/${id}`).get();
//   if (resData.success) {
//     form.value = resData.data;
//   }
// };
const openDialog = async (id: string) => {
  //获取生产订单id
  productionOrderId = id;
  form.value.id = id;
  //加载生产订单信息
  // await loadProductionOrderStep(id);
  //继续初始化表单
  initForm();
  //加载完数据再打开对话框
  await formtDialogRef.value.openDialog();
};
const closeDialog = () => {
  //关闭对话框
  formtDialogRef.value.closeDialog();
};
//添加表单关闭后触发
const dialogClosed = async () => {
  //通过重新赋值，重置表单
  form.value = {};
};
//在表单提交前要处理的事
const beforeFormSubmit = async () => {
  //封装要提交的对象。主要是封装物料批次
};
//在表单提交失败后
const afterFormSubmitFailure = async () => {
  //重新给物料批次复制，避免重新加载后失效
};
//表单提交成功后
const afterSubmitSuccess = () => {
  //表单成交提供后的回调
  emit('afterSubmitSuccess');
};
defineExpose({
  // 参考vue3官方文档，需要将函数暴露出去才能被父组件调用，setup模式是不暴露的
  openDialog,
  closeDialog
});
</script>

<style scoped></style>
